package videojuegoalpha;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;

public class Background extends JPanel implements ActionListener, Runnable {
    int velocityOption, multiplier, levelDistance, highscore, minimalDistance;
    BufferedImage baseBackground;
    Character character;
    Obstacle obstacle;
    public Image imgFondo,marco;
    Timer time;
    static int actualHeight = 219;
    static int standardHeight = 219;
    static int maxHeightValue = 160;
    Thread animator; // Brinco.   
    boolean lost = false;
    
    boolean a = false;
    boolean done2 = false;
    static Font font = new Font("SanSerif", Font.BOLD, 24);
    
   
    
    // Constructor //
    
    public Background() {
        character = new Character();
        //obstacle=new Obstacle();
        addKeyListener(new AL());
        setFocusable(true);
        ImageIcon fondo = new ImageIcon("fondo.jpg");
        ImageIcon m = new ImageIcon("marco.png");
        imgFondo = fondo.getImage();
        marco=m.getImage();
        time = new Timer(5, this);
        time.start();
        obstacle=new Obstacle(700,actualHeight);
        //en = new Enemy(700, 90, "C:/Users/NeroLux/Documents/NetBeansProjects/OurGame/enemy.png");
        //en2 = new Enemy(700, 100, "C:/Users/NeroLux/Documents/NetBeansProjects/OurGame/enemy.png");
    }
    
    // Métodos del dude //
    
        public void actionPerformed(ActionEvent e) {
        checkCollisions();
        character.move();
        repaint();
        
                
    }
//        public void paint(Graphics g) {
//        //if (lost) {
//            // frame = new JFrame("Game Over");
//            // JOptionPane.showMessageDialog(null, "YOU SUCK");
//                //////////CAMBIO:Aqui va el mensaje cuando colisiona el personaje con el fantasma.
//            //JOptionPane.showMessageDialog(null,"You suck!","Game Over",JOptionPane.WARNING_MESSAGE);
//            //System.out.println("YOU SUCK");
//        //}
//        
//        if (character.dy == 1 && done2 == false) {
//            done2 = true;
//            animator = new Thread(this);
//            animator.start();

        public void paint(Graphics g) {
            //Codigo Jimmy
            //System.out.println("Altura: "+actualHeight);
            // codigo Jimmy
                if (character.dy == 1 && done2 == false) {
                        done2 = true;
                        animator = new Thread(this);
                        animator.start();
                }
                int largoPantalla=800;
                int diferencialPantalla=74;
 
                super.paint(g);
                Graphics2D g2d = (Graphics2D) g;
 
                if ((character.getPositionX() - diferencialPantalla) % 1600 == 0)// character.getPositionX() == 590 || character.getPositionX() == 2990)
                        character.nx = 0;
                if ((character.getPositionX() - (largoPantalla+diferencialPantalla)) % 1600 == 0)// character.getPositionX() == 1790 || character.getPositionX() == 4190)
                        character.nx2 = 0;
 
                g2d.drawImage(imgFondo, largoPantalla - character.getnX2(), 0, null);                
                //Mod Jimmy
                //System.out.println("X: "+character.getPositionX());
                //System.out.println("Y: "+character.getPositionY());
                //END Mod Jimmy
                if (character.getPositionX() > diferencialPantalla) {
                        g2d.drawImage(imgFondo, largoPantalla - character.getnX(), 0, null);
                }
                if(character.actual==character.dash){
                    g2d.drawImage(character.getImage(), character.left, actualHeight+25, null); //los 25 son para recuperar el sprite al piso
                }
                else{
                    g2d.drawImage(character.getImage(), character.left, actualHeight, null);
                }
                
                g2d.setFont(font);
                g2d.setColor(Color.BLUE);
                g2d.drawImage(marco,0,0,null);
                g2d.drawString("Vidas: " + character.lifes, 680, 50);
        }
        
    private class AL extends KeyAdapter {
        public void keyReleased(KeyEvent e) {
            character.keyReleased(e);
        }
        public void keyPressed(KeyEvent e) {
            character.keyPressed(e);
        }
    }
    
    public void checkCollisions() {
        Rectangle rock = obstacle.getBounds();        
        Rectangle dude = character.getBounds();
        if (dude.intersects(rock)){
           character.isAlive = false; 
        }        
    }
    
    
    boolean maxHeight = false;
    boolean done = false;

    public void cycle() {
        ///////////////////CAMBIO: Limite de cuanto puede brincar el personaje principal. - Rocha
        if(maxHeight == false){
            actualHeight--;
        }
        
        if(actualHeight == maxHeightValue){
            maxHeight = true;
        }
        
        if(maxHeight == true && actualHeight <= standardHeight){
            actualHeight++;
            if (actualHeight == standardHeight) {
                done = true;
            }
        }
    }

    @Override
    public void run() {
        //variables para sacar longitud del salto
        int auxLongitudSaltoInicial = character.getPositionX();        
        character.keyAllowed=false; //esto evita la interferecia del teclado durante el salto
        
        long beforeTime, timeDiff, sleep;
        beforeTime = System.currentTimeMillis();
        while (done == false) {
            cycle();
            timeDiff = System.currentTimeMillis() - beforeTime;
            sleep = 10 - timeDiff;

            if (sleep < 0) {
                sleep = 2;
            }
            try {
                Thread.sleep(sleep);
            } catch (InterruptedException e) {
            }

            beforeTime = System.currentTimeMillis();
        }
        done = false;
        maxHeight = false;
        done2 = false;
        if(character.dashPressed){
            character.setImage(character.dash);
        }else{
            character.setImage(character.still);
        }        
        character.keyAllowed=true;
         //Control Longitud del salto
        System.out.println("Longitud del Salto: " + (character.getPositionX() - auxLongitudSaltoInicial));
    }

    // Métodos propios
    
    public BufferedImage showBackground(){
        return null;
    }
    
    public BufferedImage scrollBackground(){
        return null;
    }
    
    public BufferedImage generateObstacle(Obstacle obs){
        return null;
    }
    
    public int velocity(int velocityOption){
        return 0;
    }
    
    public int showHighscore(int highscore){
        return 0;
    }
    
    public BufferedImage multiplier(){
        return null;
    }
    
    public BufferedImage showScore(int score){
        return null;
    }
    
    public BufferedImage showLifes(int lifes){
        return null;
    }
    
    public BufferedImage showPowerUp(BufferedImage powerUp){
        return null;
    }
    
    // Getters y Setters //

    public BufferedImage getBaseBackground() {
        return baseBackground;
    }

    public void setBaseBackground(BufferedImage baseBackground) {
        this.baseBackground = baseBackground;
    }

    public Character getCharacter() {
        return character;
    }

    public void setCharacter(Character character) {
        this.character = character;
    }

    public int getHighscore() {
        return highscore;
    }

    public void setHighscore(int highscore) {
        this.highscore = highscore;
    }

    public int getLevelDistance() {
        return levelDistance;
    }

    public void setLevelDistance(int levelDistance) {
        this.levelDistance = levelDistance;
    }

    public int getMinimalDistance() {
        return minimalDistance;
    }

    public void setMinimalDistance(int minimalDistance) {
        this.minimalDistance = minimalDistance;
    }

    public int getVelocityOption() {
        return velocityOption;
    }

    public void setVelocityOption(int velocityOption) {
        this.velocityOption = velocityOption;
    }
}
